#include "mt4chart.h"
#include <QPainter>
#include <QDateTime>
#include <QtMath>
#include <QFontMetrics>
#include <QPainter>
#include <QDateTime>
#include <QtMath>
#include <QFontMetrics>

MT4Chart::MT4Chart(QWidget *parent) : QWidget(parent)
{
    // 初始化画笔
    m_majorPen.setColor(QColor(80, 80, 80)); // 深灰色主网格
    m_minorPen.setColor(QColor(50, 50, 50)); // 暗灰色次网格
    setGridProperties(2, 1, 5, 4, 6, 3); // 默认值
}

void MT4Chart::setPriceRange(double minPrice, double maxPrice)
{
    m_minPrice = minPrice;
    m_maxPrice = maxPrice;
    update(); // 触发重绘
}

void MT4Chart::setTimeRange(const QDateTime &startTime, const QDateTime &endTime)
{
    m_startTime = startTime;
    m_endTime = endTime;
    update();
}

void MT4Chart::setGridProperties(int majorLineWidth, int minorLineWidth,
                                 int majorHLineCount, int minorHLineCount,
                                 int majorVLineCount, int minorVLineCount)
{
    // 设置线宽
    m_majorLineWidth = majorLineWidth;
    m_minorLineWidth = minorLineWidth;

    // 设置网格数量
    m_majorHLineCount = majorHLineCount;
    m_minorHLineCount = minorHLineCount;
    m_majorVLineCount = majorVLineCount;
    m_minorVLineCount = minorVLineCount;

    // 更新画笔
    m_majorPen.setWidth(m_majorLineWidth);
    m_minorPen.setWidth(m_minorLineWidth);

    update(); // 重绘图表
}

QRectF MT4Chart::chartPlotArea() const
{
    // 计算绘图区域（留出边框用于显示刻度）
    int marginLeft = 20;   // 左侧价格刻度宽度
    int marginRight = 10;
    int marginTop = 10;
    int marginBottom = 30; // 底部时间刻度高度

    return QRectF(marginLeft, marginTop,
                  width() - marginLeft - marginRight,
                  height() - marginTop - marginBottom);
}

void MT4Chart::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // 绘制背景
    painter.fillRect(rect(), QColor(20, 20, 20)); // 深色背景

    QRectF plotArea = chartPlotArea();
    if (plotArea.isEmpty()) return;

    // 绘制主网格线（较粗）
    painter.setPen(m_majorPen);

    // 水平主网格线（价格方向）
    double priceStep = (m_maxPrice - m_minPrice) / m_majorHLineCount;
    for (int i = 0; i <= m_majorHLineCount; ++i) {
        double y = plotArea.bottom() - (i * plotArea.height() / m_majorHLineCount);
        painter.drawLine(plotArea.left(), y, plotArea.right(), y);
    }

    // 垂直主网格线（时间方向）
    qint64 timeSpan = m_startTime.secsTo(m_endTime);
    for (int i = 0; i <= m_majorVLineCount; ++i) {
        double x = plotArea.left() + (i * plotArea.width() / m_majorVLineCount);
        painter.drawLine(x, plotArea.top(), x, plotArea.bottom());
    }

    // 绘制次网格线（较细，在主网格之间）
    painter.setPen(m_minorPen);

    // 水平次网格线
    double minorHStep = 1.0 / (m_minorHLineCount + 1);
    for (int i = 0; i <= m_majorHLineCount; ++i) {
        for (int j = 1; j <= m_minorHLineCount; ++j) {
            double ratio = i + j * minorHStep;
            double y = plotArea.bottom() - (ratio * plotArea.height() / m_majorHLineCount);
            painter.drawLine(plotArea.left(), y, plotArea.right(), y);
        }
    }

    // 垂直次网格线
    double minorVStep = 1.0 / (m_minorVLineCount + 1);
    for (int i = 0; i <= m_majorVLineCount; ++i) {
        for (int j = 1; j <= m_minorVLineCount; ++j) {
            double ratio = i + j * minorVStep;
            double x = plotArea.left() + (ratio * plotArea.width() / m_majorVLineCount);
            painter.drawLine(x, plotArea.top(), x, plotArea.bottom());
        }
    }
}



void testMt4Chat(){

    MT4Chart chart;
    // 设置价格范围（例如EUR/USD价格）
    chart.setPriceRange(1.0800, 1.0950);
    // 设置时间范围（过去24小时）
    QDateTime endTime = QDateTime::currentDateTime();
    QDateTime startTime = endTime.addDays(-1);
    chart.setTimeRange(startTime, endTime);

    chart.resize(800, 600);
    chart.show();
}
